<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25
 * Time: 10:18
 */

namespace app\qgroup\controller;


use app\common\Enum\CommonEnum;
use app\common\util\ReturnMsgUtil;
use app\qgroup\model\GroupList;
use app\qgroup\model\GroupMemberMapping;
use app\qgroup\model\QqCodeMapping;
use app\qgroup\service\ConfigService;
use think\Exception;

class Config
{
    use ReturnMsgUtil;

    /**
     * 初始化酷Q状态
     * @return string
     * @throws \think\exception\DbException
     */
    public function init()
    {
        $qq = input('qq');
        $groups = input('group');
        $qcm = QqCodeMapping::get(['qq'=>$qq]);
        if (!$qcm){
            $qcm = ConfigService::init($qq,$groups,$existGroups);
        }
        $gls = GroupList::all(['qq'=>$qq]);
        return $this->successInfo('成功',$qcm,["groups"=>$gls,'existGroups'=>isset($existGroups)?$existGroups:null]);
    }

    /**
     * 更新QQ管理者
     * @return string
     * @throws \think\exception\DbException
     */
    public function update()
    {
        $masterQQ = input('masterQQ');
        $qq = input('qq');
        $qcm = QqCodeMapping::get(['qq'=>$qq]);
        $r = $qcm->save(['masterQQ'=>$masterQQ]);
        if ($r === false){
            return $this->errorInfo('管理者QQ更新失败');
        }
        return $this->successInfo('管理者QQ更新成功');
    }

    /**
     * 更新群管
     * @return string
     * @throws \think\exception\DbException
     */
    public function updateGroup()
    {
        $masterQQ = input('masterQQ');
        $qq = input('qq');
        $group = input('group');
        $gl = GroupList::get(['qq'=>$qq,'group'=>$group]);
        if (!$gl){
            $gl = GroupList::create([
                'qq'=>$qq
                ,'group'=>$group
                ,'masterQQ'=>$masterQQ
                ,'status'=>CommonEnum::COMMON_VALID
            ]);
            if ($gl){
                return $this->successInfo('群管理QQ更新成功');
            }else{
                return $this->errorInfo('群管理QQ更新失败');
            }
        }else{
            $r = $gl->save(['masterQQ'=>$masterQQ]);
            if ($r === false){
                return $this->errorInfo('群管理QQ更新失败');
            }else{
                return $this->successInfo('群管理QQ更新成功');
            }
        }
    }

    /**
     * 更新群成员信息
     * @throws \think\exception\DbException
     */
    public function updateGroupMemberInfo()
    {
        $qq = input('qq');
        $qqs = input('qqs');
        $group = input('group');
        $gl = GroupList::get(['qq'=>$qq,'group'=>$group]);
        if (!$gl){

        }
        foreach ($qqs as $qq){
            $qqInfo = json_decode($qq,1);
            try{
                $gmm = ConfigService::createQqGroupMapping($gl,$qqInfo);
            }catch (Exception $exception){
                continue;
            }
        }
        return $this->successInfo('群成员信息更新成功');
    }

    public function delGroupMenberInfo()
    {
        $qq = input('qq');
        $times = input('sendTime');
        $group = input('fromGroup');
        $adminQQ = input('fromQQ');
        $qqs = input('beingOperateQQ');
        $status = input('subtype');

        $gl = GroupList::get(['group'=>$group]);
        $times = date('Y-m-d H:i:s',$times);
        $gmm = GroupMemberMapping::get(['qq'=>$qqs,'group_list_id'=>$gl->id]);
        $r = $gmm->save(['status'=>$status == 1 ? 20 : 30,'outing_group_time'=>$times]);
        return $this->successInfo('群成员信息更新成功');
    }
}